package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Role;
import cn.bdqn.t354.pojo.User;
import cn.bdqn.t354.service.RoleService;
import cn.bdqn.t354.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    /**
     * 列表展示
     * @param usrName
     * @param roleId
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String usrName, Long roleId,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model) {
        Integer pageSize = 3;
        IPage<User> usersByPage = userService.getUsersByPage(usrName, roleId, pageIndex, pageSize);
        model.addAttribute("userPager", usersByPage);

        //过滤条件回显
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);

        return "user/list";

    }

    /**
     * 跳转新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model) {
        List<Role> allRoles = roleService.findAllRoles();
        model.addAttribute("roles", allRoles);
        return "user/add";
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @RequestMapping("/save")
    public String save(User user) {
        int i = userService.addUser(user);
        if (i > 0) {
            return "redirect:/user/list";
        } else {
            return "redirect:/user/add";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param usrId
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long usrId, Model model) {
        //根据id查询用户信息
        User user = userService.getUser(usrId);
        model.addAttribute("user", user);
        //查询所有的角色用户角色下拉框的展示
        List<Role> roles = roleService.findAllRoles();
        model.addAttribute("roles", roles);
        return "user/edit";
    }

    /**
     * 修改操作
     *
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public String update(User user) {
        //根据id查询用户信息
        int rs = userService.updateUser(user);
        if (rs > 0) {
            //跳转到用户列表页面
            return "redirect:/user/list";
        } else {
            //跳转到用户修改页面
            return "redirect:/user/edit";
        }
    }

    /**
     * 删除操作
     *
     * @param usrId
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(Long usrId, Model model) {
        Map<String, Object> map = new HashMap<>();
        int rs = userService.deleteUser(usrId);
        if (rs > 0) {
            map.put("delResult", true);
        } else {
            map.put("delResult", false);
        }
        return map;
    }

}
